package com.weavernorth.action.single;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 单项激励流程是否有重复提交,执行节点后action时,当前数据的状态为 保存
 */
public class CheckSingleRepeatAction implements Action {
    private static Log log = LogFactory.getLog(CheckSingleRepeatAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {
        //请求id
        String strRequestid = requestInfo.getRequestid();
        //表名
        String strTable = requestInfo.getRequestManager().getBillTableName();
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsSingle = new RecordSet();
        //查询表单数据
        String sql = "select fm.rq , fmd.dxjllx ,fmd.xm,hr.lastname from  " + strTable + " fm," + strTable + "_dt1 fmd,hrmresource hr  where fmd.xm=hr.id and fm.id=fmd.mainid and  fm.requestid=?";
        log.info("当前表单数据:" + sql + ",requestid:" + strRequestid);
        rs.executeQuery(sql, strRequestid);
        while (rs.next()) {
            //获取到奖励日期
            String strRq = Util.null2String(rs.getString("rq"));
            //获取到明细表的单项激励类型
            String strDxjllx = Util.null2String(rs.getString("dxjllx"));
            //姓名id
            String strUserid = Util.null2String(rs.getString("xm"));
            //名称
            String strLastname=Util.null2String(rs.getString("lastname"));
            //根据姓名和奖励日期,和单项激励类型判断是否有已存在的,存在则阻止提交
            String sqlCheck = "select 1 from " + strTable + " fm," + strTable + "_dt1 fmd,workflow_requestbase wr  where fm.id=fmd.mainid and  fm.requestid=wr.requestid " +
                    " and substr(fm.rq, 0, 7)=? and fmd.dxjllx=? and fmd.xm=?  and (wr.currentnodetype!='0' or wr.requestid=?)";
            log.info("查询是否存在重复:" + sqlCheck + ",strRq:" + strRq.substring(0, 7) + ",strDxjllx:" + strDxjllx + ",strUserid:" + strUserid);
            rsSingle.executeQuery(sqlCheck, strRq.substring(0, 7), strDxjllx, strUserid, strRequestid);
            if (rsSingle.getCounts()>1) {
                requestInfo.getRequestManager().setMessagecontent("您提交的当前奖励月份中,姓名为:"+strLastname+"的单项激励类型有重复");
                return FAILURE_AND_CONTINUE;

            }

        }
        return SUCCESS;
    }

}